package com.hengtongxing.hejiayun.mine;

import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.hengtongxing.hejiayun.R;
import com.hengtongxing.hejiayun.base.BaseActivity;
import com.hengtongxing.hejiayun.base.DataCallBack;
import com.hengtongxing.hejiayun.bean.BaseResponseBean;
import com.hengtongxing.hejiayun.bean.MyHouseListBean;
import com.hengtongxing.hejiayun.dialog.ChooseBuildingDialog;
import com.hengtongxing.hejiayun.dialog.ChooseCommunityDialog;
import com.hengtongxing.hejiayun.dialog.ChooseFloorDialog;
import com.hengtongxing.hejiayun.dialog.ChooseHouseDialog;
import com.hengtongxing.hejiayun.dialog.ChooseUnitDialog;
import com.hengtongxing.hejiayun.event.HouseEvent;
import com.hengtongxing.hejiayun.mine.model.IMineModel;
import com.hengtongxing.hejiayun.mine.model.MineModelImpl;
import com.hengtongxing.hejiayun.utils.ToastUtil;
import com.hengtongxing.hejiayun.widget.AppTitleBar;
import com.hengtongxing.hejiayun.widget.CommonNoTitleDialog;

import org.greenrobot.eventbus.EventBus;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import ch.ielse.view.SwitchView;

public class AddHouseActivity extends BaseActivity {


    @BindView(R.id.title_bar)
    AppTitleBar titleBar;
    @BindView(R.id.tv_community)
    TextView tvCommunity;
    @BindView(R.id.ll_community)
    LinearLayout llCommunity;
    @BindView(R.id.tv_building)
    TextView tvBuilding;
    @BindView(R.id.ll_building)
    LinearLayout llBuilding;
    @BindView(R.id.tv_unit)
    TextView tvUnit;
    @BindView(R.id.ll_unit)
    LinearLayout llUnit;
    @BindView(R.id.tv_floor)
    TextView tvFloor;
    @BindView(R.id.ll_floor)
    LinearLayout llFloor;
    @BindView(R.id.tv_house)
    TextView tvHouse;
    @BindView(R.id.ll_house)
    LinearLayout llHouse;
    @BindView(R.id.switchview)
    SwitchView switchview;
    @BindView(R.id.tv_commit)
    TextView tvCommit;
    private IMineModel mineModel;
    private String cid;
    private String bid;
    private String uid;
    private String fid;
    private String hid;
    private int is_def;
    private MyHouseListBean.DataBean dataBean;
    private CommonNoTitleDialog.Builder dialogBuild;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_house);
        ButterKnife.bind(this);
        initView();
    }

    public void initView() {
        dataBean = (MyHouseListBean.DataBean) getIntent().getSerializableExtra("dataBean");
        if (dataBean != null) {
            titleBar.setTitle("编辑房屋");
            titleBar.setRightTxt("删除");
            titleBar.setRightClickListener(() -> {
                dialogBuild = new CommonNoTitleDialog.Builder(mActivity);
                dialogBuild.setMessage("是否删除此房屋?");
                dialogBuild.setPositiveButton("确定", v -> {
                    dialogBuild.dismiss();
                    requestDelHouses();
                });
                dialogBuild.setNegativeButton("取消", v -> dialogBuild.dismiss());
                dialogBuild.setCancelable(false);
                dialogBuild.show();
            });
            tvCommunity.setText(dataBean.getComm_name());
            tvBuilding.setText(dataBean.getBuilding());
            tvUnit.setText(dataBean.getUnit_name());
            tvFloor.setText(dataBean.getFloor());
            tvHouse.setText(dataBean.getHose_number());


            cid = dataBean.getCid() + "";
            bid = dataBean.getBid() + "";
            uid = dataBean.getUid() + "";
            fid = dataBean.getFid() + "";
            hid = dataBean.getHid() + "";

            if (dataBean.getIs_def() == 1) {
                switchview.setOpened(true);
            }
        }
        mineModel = new MineModelImpl();
    }


    @OnClick({R.id.ll_community, R.id.ll_building, R.id.ll_unit, R.id.ll_floor, R.id.ll_house, R.id.tv_commit})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.ll_community:
                ChooseCommunityDialog dialog1 = ChooseCommunityDialog.newInstance("");
                dialog1.show(getSupportFragmentManager(), "dialog");
                dialog1.setOnCallBackListener((name, id) -> {
                    tvCommunity.setText(name);
                    cid = id;
                    bid = "";
                    tvBuilding.setText("");
                    uid = "";
                    tvUnit.setText("");
                    fid = "";
                    tvFloor.setText("");
                    hid = "";
                    tvHouse.setText("");
                });
                break;
            case R.id.ll_building:
                if (TextUtils.isEmpty(cid)) {
                    ToastUtil.showShort("请选择您的小区");
                    return;
                }
                ChooseBuildingDialog dialog2 = ChooseBuildingDialog.newInstance(cid);
                dialog2.show(getSupportFragmentManager(), "dialog");
                dialog2.setOnCallBackListener((name, id) -> {
                    tvBuilding.setText(name);
                    bid = id;
                    uid = "";
                    tvUnit.setText("");
                    fid = "";
                    tvFloor.setText("");
                    hid = "";
                    tvHouse.setText("");
                });
                break;
            case R.id.ll_unit:
                if (TextUtils.isEmpty(cid)) {
                    ToastUtil.showShort("请选择您的小区");
                    return;
                }
                if (TextUtils.isEmpty(bid)) {
                    ToastUtil.showShort("请选择楼栋");
                    return;
                }
                ChooseUnitDialog dialog3 = ChooseUnitDialog.newInstance(cid, bid);
                dialog3.show(getSupportFragmentManager(), "dialog");
                dialog3.setOnCallBackListener((name, id) -> {
                    tvUnit.setText(name);
                    uid = id;
                    fid = "";
                    tvFloor.setText("");
                    hid = "";
                    tvHouse.setText("");
                });
                break;
            case R.id.ll_floor:
                if (TextUtils.isEmpty(cid)) {
                    ToastUtil.showShort("请选择您的小区");
                    return;
                }
                if (TextUtils.isEmpty(bid)) {
                    ToastUtil.showShort("请选择楼栋");
                    return;
                }
                if (TextUtils.isEmpty(uid)) {
                    ToastUtil.showShort("请选择单元");
                    return;
                }
                ChooseFloorDialog dialog4 = ChooseFloorDialog.newInstance(cid, bid, uid);
                dialog4.show(getSupportFragmentManager(), "dialog");
                dialog4.setOnCallBackListener((name, id) -> {
                    tvFloor.setText(name);
                    fid = id;
                    hid = "";
                    tvHouse.setText("");
                });
                break;
            case R.id.ll_house:
                if (TextUtils.isEmpty(cid)) {
                    ToastUtil.showShort("请选择您的小区");
                    return;
                }
                if (TextUtils.isEmpty(bid)) {
                    ToastUtil.showShort("请选择楼栋");
                    return;
                }
                if (TextUtils.isEmpty(uid)) {
                    ToastUtil.showShort("请选择单元");
                    return;
                }
                if (TextUtils.isEmpty(fid)) {
                    ToastUtil.showShort("请选择楼层");
                    return;
                }
                ChooseHouseDialog dialog5 = ChooseHouseDialog.newInstance(cid, bid, uid, fid);
                dialog5.show(getSupportFragmentManager(), "dialog");
                dialog5.setOnCallBackListener((name, id) -> {
                    tvHouse.setText(name);
                    hid = id;
                });
                break;
            case R.id.tv_commit:
                if (switchview.isOpened()) {
                    is_def = 1;
                }
                if (TextUtils.isEmpty(cid)) {
                    ToastUtil.showShort("请选择您的小区");
                    return;
                }
                if (TextUtils.isEmpty(bid)) {
                    ToastUtil.showShort("请选择楼栋");
                    return;
                }
                if (TextUtils.isEmpty(uid)) {
                    ToastUtil.showShort("请选择单元");
                    return;
                }
                if (TextUtils.isEmpty(fid)) {
                    ToastUtil.showShort("请选择楼层");
                    return;
                }
                if (TextUtils.isEmpty(hid)) {
                    ToastUtil.showShort("请选择门牌号");
                    return;
                }
                if (dataBean != null) {
                    requestUpMyHouse();
                }
                requestHouseAdd();
                break;
        }
    }

    public void requestHouseAdd() {
        mineModel.requestHouseAdd(cid, bid, uid, fid, hid, String.valueOf(is_def), new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("添加成功");
                EventBus.getDefault().post(new HouseEvent());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {
                ToastUtil.showShort(msg);
            }
        });
    }


    public void requestUpMyHouse() {
        mineModel.requestUpMyHouse(dataBean.getId() + "", cid, bid, uid, fid, hid, String.valueOf(is_def), new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("修改成功");
                EventBus.getDefault().post(new HouseEvent());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {
                ToastUtil.showShort(msg);
            }
        });
    }

    public void requestDelHouses() {
        mineModel.requestDelHouses(dataBean.getId() + "", new DataCallBack<BaseResponseBean>() {
            @Override
            public void onSuccessful(BaseResponseBean bean) {
                ToastUtil.showShort("删除成功");
                EventBus.getDefault().post(new HouseEvent());
                finish();
            }

            @Override
            public void onFailed(String msg, String code) {

            }
        });
    }
}
